package day02;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.function.Consumer;

/*
 *文件名: HashSetDemo
 *创建者:
 *创建时间:2025/5/20 10:58
 *描述:
 */
public class HashSetDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("A");
        list.add("A");
        System.out.println(list);

        ArrayList<String> newlist = new ArrayList<>(new HashSet<>(list));

        System.out.println(newlist);

        //1创建HashSet集合对象    不重复，无顺序
        HashSet<String> hashSet = new HashSet<>();//HashSet自动去重
        //2保存数据
        hashSet.add("a");
        hashSet.add("a");
        hashSet.add("c");
        hashSet.add("b");
        hashSet.add("d");

        System.out.println(hashSet);

        //取数据
        Iterator<String> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
//        for (int i = 0; i < hashSet.size(); i++) {
//
//        }    HashSet不可用for简单查询
        //4.取数据（增强for循环）
        for (String name : hashSet) {
            System.out.println("name: " + name);
        }
        int[] array ={1,2,3,4,5,6,7,8,9};
        for (int i : array) {
            System.out.println(i);
        }
        //5.取数据(Lambda)
        hashSet.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });

        hashSet.forEach(System.out::println);

        Student student = new Student("张三",28);
        System.out.println(student);
    }
}